﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class ThucTap
    {
         //ID
        public String maThucTap;
        public String maDiaDiem;

        //Properties
        DateTime ngayBatDau;
        DateTime ngayKetThuc;
        String noiDungThucTap;

        // object
        DiaDiemThucTap diaDiemThucTap;

        //database
        Database myDatabase = Database.shareDatabase;

        public DiaDiemThucTap getDiaDiemThucTap()
        {
            return new DiaDiemThucTap(maDiaDiem);
        }
        public ThucTap(DateTime _ngayBatDau, DateTime _ngayKetThuc, String _noiDungThucTap,DiaDiemThucTap _diaDiemThucTap)
        {
            //ID
            maThucTap = Database.getID("ThucTap", "MaThucTap");
            maDiaDiem = _diaDiemThucTap.maDiaDiem;

            //Properties
            ngayBatDau = _ngayBatDau;
            ngayKetThuc = _ngayKetThuc;
            noiDungThucTap = _noiDungThucTap;

            // object
            diaDiemThucTap = _diaDiemThucTap;

            
        }
        public ThucTap(String _maThucTap)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM ThucTap WHERE MaThucTap = '" + _maThucTap + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                //ID
                maThucTap = table.Rows[0]["MaThucTap"].ToString();
                maDiaDiem = table.Rows[0]["MaDiaDiem"].ToString();

                //Properties
                ngayBatDau = table.Rows[0].Field<DateTime>("NgayBatDau");
                ngayKetThuc = table.Rows[0].Field<DateTime>("NgayKetThuc");
                noiDungThucTap = table.Rows[0]["NoiDungThucTap"].ToString(); 

            }
        }
        public Boolean Save()
        {
            diaDiemThucTap.Save();

            String sqlThucTapSinh = "INSERT INTO ThucTap VALUES('"
                + maThucTap + "','"
                + Database.sqlDateTime(ngayBatDau)
                + "','" + Database.sqlDateTime(ngayKetThuc)
                + "','" + maDiaDiem
                + "',N'" + noiDungThucTap + "')";
            QueryResult result = myDatabase.Query(sqlThucTapSinh);

            if (result.ReturnCode == 409)
            {
                return false;
            }
            else
            {
                
                return true;
            }
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("ThucTap", "MaThucTap", maThucTap);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE ThucTap SET " 
                    + "NgayBatDau = '" + Database.sqlDateTime(ngayBatDau)
                    + "', NgayKetThuc = '" + Database.sqlDateTime(ngayKetThuc)
                    + "', NoiDungThucTap = N'" + noiDungThucTap
                    + "' WHERE MaThucTap = '" + maThucTap + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
                else
                {
                    return true;
                }

            }
            return false;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM ThucTap WHERE MaThucTap = '" + maThucTap + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                diaDiemThucTap.Delete();
                return true;
            }
        }
    }
}
